import people.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import mysqloperate.*;

public class registerpage extends JFrame {

    private JLabel username_Label;
    private JLabel password_Label;
    private JLabel repassword_Label;
    private JTextField username_Text;
    private JPasswordField password_Text;
    private JPasswordField repassword_Text;
    private JButton register_Button;
    private JButton cancel_Button;
    private JFrame jf;

    public registerpage(){
        init();
        jf=this;
        Box b=Box.createVerticalBox();

        Box b1=Box.createHorizontalBox();
        username_Label=new JLabel("用 户 名");
        username_Text=new JTextField(20);
        b1.add(username_Label);
        b1.add(username_Text);

        Box b2=Box.createHorizontalBox();
        password_Label=new JLabel("密   码");
        password_Text=new JPasswordField(20);
        b2.add(password_Label);
        b2.add(password_Text);

        Box b3=Box.createHorizontalBox();
        repassword_Label=new JLabel("确认密码");
        repassword_Text=new JPasswordField(20);
        b3.add(repassword_Label);
        b3.add(repassword_Text);

        register_Button=new JButton("注册");
        register_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String zhang=username_Text.getText().trim();
                String mi=new String(password_Text.getPassword());
                String remi=new String(repassword_Text.getPassword());
               if(zhang.equals("")){
                   JOptionPane.showMessageDialog(jf, "请 输 入 账 号","",JOptionPane.WARNING_MESSAGE);
                   return ;
               }
               if(mi.equals("")){
                   JOptionPane.showMessageDialog(jf, "请 输 入 密 码","",JOptionPane.WARNING_MESSAGE);
                   return ;
               }
               if(remi.equals("")){
                   JOptionPane.showMessageDialog(jf, "请 再 次 输 入 密 码","",JOptionPane.WARNING_MESSAGE);
                   return ;
               }
               if(!(mi.equals(remi))){
                   JOptionPane.showMessageDialog(jf, "输入的两次密码不一致","",JOptionPane.WARNING_MESSAGE);
                   return ;
               }
               User use=new User();
               use.setId(zhang);
               use.setPassword(mi);

               addusermysql add=new addusermysql();
                try {
                    add.adduser(use);
                    JOptionPane.showMessageDialog(jf, "注册成功","",JOptionPane.INFORMATION_MESSAGE);
                    jf.dispose();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(jf, "注册失败","",JOptionPane.WARNING_MESSAGE);
                    throw new RuntimeException(ex);
                }

            }
        });
        cancel_Button=new JButton("取消");
        cancel_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });

        b.add(b1);
        b.add(Box.createVerticalStrut(50));
        b.add(b2);
        b.add(Box.createVerticalStrut(50));
        b.add(b3);
        add(b);
        add(register_Button);
        add(cancel_Button);

        setVisible(true);
    }
    void init(){
        setTitle("账号注册");
        setBounds(500,200,300,400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new FlowLayout());
        setAlwaysOnTop(true);
        setResizable(false);

        Container con=this.getContentPane();
        con.setBackground(Color.WHITE);
    }
}
